﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace DALL.SysManage
{
    public class T_PUB_DICTIONARY
    {
        public T_PUB_DICTIONARY()
        { }

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool IsExistName(int PARENT_ID, string NAME)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_PUB_DICTIONARY where NAME=@NAME and PARENT_ID=@PARENT_ID and FLAG=1");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NAME", DbType.String, NAME);
            db.AddInParameter(dbCommand, "PARENT_ID", DbType.Int32, PARENT_ID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        #endregion  Method


          #region 获取同一父结点的子结点
        /// <summary>
        /// 获取同一父结点的子结点
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public DataTable GetParentOfChildList(string strWhere)
        {

            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("select ID,NAME,PARENT_ID,ORDER_ID,REMARK,FLAG from T_PUB_DICTIONARY ");
            //strSql.Append(" where FLAG=1 and PARENT_ID=@PARENT_ID  order by ORDER_ID ");
            //Database db = DatabaseFactory.CreateDatabase();
            //DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            //db.AddInParameter(dbCommand, "@PARENT_ID", DbType.Int32, PARENT_ID);
            //return db.ExecuteDataSet(CommandType.Text, strSql.ToString()).Tables[0];
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,NAME,PARENT_ID,ORDER_ID,REMARK,FLAG,Type ");
            strSql.Append(" FROM T_PUB_DICTIONARY ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString()).Tables[0];
        }
          #endregion


          #region 修改结点序号
        /// <summary>
        /// 修改结点序号
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="OrderID"></param>
        /// <returns></returns>
        public bool UpdateOrderID(int ID, int ORDER_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_DICTIONARY set ");
            strSql.Append("ORDER_ID=@ORDER_ID");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            db.AddInParameter(dbCommand, "ORDER_ID", DbType.Int32, ORDER_ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
          #endregion


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteByFlag(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_DICTIONARY set ");
            strSql.Append("FLAG= 0");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_DICTIONARY model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_DICTIONARY(");
            strSql.Append("NAME,PARENT_ID,ORDER_ID,REMARK,FLAG)");

            strSql.Append(" values (");
            strSql.Append("@NAME,@PARENT_ID,@ORDER_ID,@REMARK,@FLAG)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NAME", DbType.String, model.NAME);
            db.AddInParameter(dbCommand, "PARENT_ID", DbType.Int32, model.PARENT_ID);
            db.AddInParameter(dbCommand, "ORDER_ID", DbType.Int32, model.ORDER_ID);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "FLAG", DbType.Boolean, model.FLAG);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysManage.T_PUB_DICTIONARY model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_DICTIONARY set ");
            strSql.Append("NAME=@NAME,");
            strSql.Append("PARENT_ID=@PARENT_ID,");
            strSql.Append("ORDER_ID=@ORDER_ID,");
            strSql.Append("REMARK=@REMARK,");
            strSql.Append("FLAG=@FLAG");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, model.ID);
            db.AddInParameter(dbCommand, "NAME", DbType.String, model.NAME);
            db.AddInParameter(dbCommand, "PARENT_ID", DbType.Int32, model.PARENT_ID);
            db.AddInParameter(dbCommand, "ORDER_ID", DbType.Int32, model.ORDER_ID);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "FLAG", DbType.Boolean, model.FLAG);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_DICTIONARY ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_DICTIONARY GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,NAME,PARENT_ID,ORDER_ID,REMARK,FLAG,Type,Field from T_PUB_DICTIONARY ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            Model.SysManage.T_PUB_DICTIONARY model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,NAME,PARENT_ID,ORDER_ID,REMARK,FLAG,Type,Field ");
            strSql.Append(" FROM T_PUB_DICTIONARY ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_DICTIONARY> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,NAME,PARENT_ID,ORDER_ID,REMARK,FLAG,Type,Field ");
            strSql.Append(" FROM T_PUB_DICTIONARY ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_DICTIONARY> list = new List<Model.SysManage.T_PUB_DICTIONARY>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_DICTIONARY ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_DICTIONARY model = new Model.SysManage.T_PUB_DICTIONARY();
            object ojb;
            ojb = dataReader["ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ID = (int)ojb;
            }
            model.NAME = dataReader["NAME"].ToString();
            ojb = dataReader["PARENT_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PARENT_ID = (int)ojb;
            }
            ojb = dataReader["ORDER_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ORDER_ID = (int)ojb;
            }
            model.REMARK = dataReader["REMARK"].ToString();
            ojb = dataReader["FLAG"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.FLAG = (bool)ojb;
            }
            model.Type = dataReader["Type"].ToString();
            model.Field = dataReader["Field"].ToString();
            return model;
        }

        #endregion  Method
    }
}
